package com.inossem.allotin.utils;

import android.app.Activity;
import android.app.Dialog;
import android.graphics.Point;
import android.support.v4.content.ContextCompat;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.inossem.allotin.R;
import com.inossem.allotin.utils.calendarview.Calendar;
import com.inossem.allotin.utils.calendarview.CalendarLayout;
import com.inossem.allotin.utils.calendarview.CalendarView;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * ----------
 * Author: Leo
 * Date: 2018/11/23
 * Email: 18304097708@163.com
 * Description:
 * ----------
 */
public class TimeDialogUtil implements CalendarView.OnCalendarSelectListener, CalendarView.OnYearChangeListener, View.OnClickListener {
    private int mYear;
    private Activity activity;
    private PopupWindow popupWindow;
    private TimeCallBackInterf timeCallBackInterf;
    private TimeStampCallBack timeStampCallBack;
    private TextView mTextMonthDay;
    private TextView mTextYear;
    private TextView mTextLunar;
    private CalendarView mCalendarView;
    private CalendarLayout mCalendarLayout;
    private Dialog dialog;

    public TimeDialogUtil(Activity activity, TimeCallBackInterf timeCallBackInterf) {
        this.timeCallBackInterf = timeCallBackInterf;
        this.activity = activity;
    }

    public TimeDialogUtil(Activity activity, Dialog dialog, TimeCallBackInterf timeCallBackInterf) {
        this.timeCallBackInterf = timeCallBackInterf;
        this.activity = activity;
        this.dialog = dialog;
    }

    public TimeDialogUtil(Activity activity, Dialog dialog, TimeStampCallBack timeStampCallBack) {
        this.timeStampCallBack = timeStampCallBack;
        this.activity = activity;
        this.dialog = dialog;
    }

    public void showDialog() {
        View inflate = LayoutInflater.from(activity).inflate(R.layout.activity_time_dialog, null, false);
        popupWindow = new PopupWindow(inflate, WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT, true);
        Display defaultDisplay = activity.getWindowManager().getDefaultDisplay();
        Point point = new Point();
        defaultDisplay.getSize(point);
        int needHeight = (int) (point.y * 0.6);//最大高度为屏幕的0.82倍
        popupWindow.setHeight(needHeight);
        popupWindow.setWidth((int) (point.x * 0.6));
        popupWindow.setFocusable(true);
        popupWindow.setOutsideTouchable(true);
        popupWindow.setBackgroundDrawable(ContextCompat.getDrawable(activity, R.drawable.popwindiw_round));
        popupWindow.setAnimationStyle(R.style.popmenu_animation);
        if (null == dialog) {
            popupWindow.showAtLocation(activity.getWindow().getDecorView(), Gravity.CENTER, 0, 0);
        } else {
            popupWindow.showAtLocation(dialog.getWindow().getDecorView(), Gravity.CENTER, 0, 0);
        }
        initView(inflate);
    }

    public void showDialogPop() {
        View inflate = LayoutInflater.from(activity).inflate(R.layout.activity_time_dialog, null, false);
        popupWindow = new PopupWindow(inflate, WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT, true);
        Display defaultDisplay = activity.getWindowManager().getDefaultDisplay();
        Point point = new Point();
        defaultDisplay.getSize(point);
        int needHeight = (int) (point.y * 0.6);//最大高度为屏幕的0.82倍
        popupWindow.setHeight(needHeight);
        popupWindow.setWidth((int) (point.x * 0.6));
        popupWindow.setFocusable(true);
        popupWindow.setOutsideTouchable(true);
        popupWindow.setBackgroundDrawable(ContextCompat.getDrawable(activity, R.drawable.style_dialog_round));
        popupWindow.setAnimationStyle(R.style.popmenu_animation);
        popupWindow.showAtLocation(dialog.getWindow().getDecorView(), Gravity.CENTER, 0, 0);
        initView(inflate);
    }

    private void initView(View dialogView) {
        mTextMonthDay = dialogView.findViewById(R.id.tv_month_day);
        mTextYear = dialogView.findViewById(R.id.tv_year);
        mTextLunar = dialogView.findViewById(R.id.tv_lunar);
        mCalendarView = dialogView.findViewById(R.id.calendarView);
        Display defaultDisplay = activity.getWindowManager().getDefaultDisplay();
        Point point = new Point();
        defaultDisplay.getSize(point);
        mCalendarView.setCalendarItemHeight(point.y / 11); // 设置行高度
        mCalendarView.setFixMode();
        mCalendarView.setMonthViewScrollable(true);
        mCalendarView.setYearViewScrollable(true);
        TextView mTextCurrentDay = dialogView.findViewById(R.id.tv_current_day);
        mTextMonthDay.setOnClickListener(v -> {
            if (!mCalendarLayout.isExpand()) {
                mCalendarView.showYearSelectLayout(mYear);
                return;
            }
            mCalendarView.showYearSelectLayout(mYear);
            mTextLunar.setVisibility(View.GONE);
            mTextYear.setVisibility(View.GONE);
            mTextMonthDay.setText(String.valueOf(mYear));
        });
        dialogView.findViewById(R.id.fl_current).setOnClickListener(v -> mCalendarView.scrollToCurrent());
        mCalendarLayout = dialogView.findViewById(R.id.calendarLayout);
        mCalendarView.setOnCalendarSelectListener(this);
        mCalendarView.setOnYearChangeListener(this);
        mTextYear.setText(String.valueOf(mCalendarView.getCurYear()));
        mYear = mCalendarView.getCurYear();
        mTextMonthDay.setText(String.valueOf(mCalendarView.getCurMonth()).concat("月").concat(String.valueOf(mCalendarView.getCurDay()).concat("日")));
        mTextLunar.setText("今日");
        mTextCurrentDay.setText(String.valueOf(mCalendarView.getCurDay()));
    }

    @Override
    public void onClick(View v) {

    }

    @Override
    public void onYearChange(int year) {
        mTextMonthDay.setText(String.valueOf(year));
    }

    @Override
    public void onCalendarOutOfRange(Calendar calendar) {
    }

    @Override
    public void onCalendarSelect(Calendar calendar, boolean isClick) {
        mTextLunar.setVisibility(View.VISIBLE);
        mTextYear.setVisibility(View.VISIBLE);
        mTextMonthDay.setText(String.valueOf(calendar.getMonth()).concat("月").concat(String.valueOf(calendar.getDay()).concat("日")));
        mTextYear.setText(String.valueOf(calendar.getYear()));
        mTextLunar.setText(calendar.getLunar());
        mYear = calendar.getYear();

        if (isClick) {
            if (timeCallBackInterf != null)
                timeCallBackInterf.timeCallBack(calendar.getYear(), calendar.getMonth(), calendar.getDay());
            if (timeStampCallBack != null)
                timeStampCallBack.timeStamp(timeStamp2Date(calendar.getTimeInMillis(), ""));
            popupWindow.dismiss();
        }
    }

    public interface TimeCallBackInterf {
        void timeCallBack(int year, int mouth, int day);
    }

    public interface TimeStampCallBack {
        void timeStamp(String time);
    }

    public static String timeStamp2Date(long time, String format) {
        if (format == null || format.isEmpty()) {
            format = "yyyy-MM-dd";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(new Date(time));
    }
}